package com.baba.vote.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author lyd
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel()
public class PageReturnDto<T> {

    @ApiModelProperty(notes = "是否成功")
    private boolean success = true;

    @ApiModelProperty(notes = "返回状态码")
    private int code = 200;

    @ApiModelProperty(notes = "异常信息")
    private String message;

    @ApiModelProperty(notes = "当前页")
    private int curPage;

    @ApiModelProperty(notes = "页面总数")
    private int pageCount = 1;

    @ApiModelProperty(notes = "页面大小")
    private int pageSize;

    @ApiModelProperty(notes = "总条目数")
    private long count = 0;

    @ApiModelProperty(notes = "是否有记录")
    private boolean hasRecords = true;

    @ApiModelProperty(notes = "记录详情")
    private T results;


    public static <T> PageReturnDto<T> formatPage(T result, PagingDto pagingDto) {
        PageReturnDto pageReturnDto = PageReturnDto.builder()
            .curPage(pagingDto.getPageNumber())
            .pageCount(pagingDto.getPageCount())
            .count(pagingDto.getCount())
            .results(result)
            .success(true)
            .hasRecords(true)
            .pageSize(pagingDto.getPageSize())
            .code(200)
            .build();

        return pageReturnDto;
    }

    public static <T> PageReturnDto<T> formatFail(String message) {
        PageReturnDto pageReturnDto = PageReturnDto.builder()
            .results(message)
            .code(VoteErrorCode.SYS_ERR.getIndex())
            .success(false)
            .hasRecords(false)
            .build();
        return pageReturnDto;
    }


}
